package com.myboot.demo.aspect;

import com.myboot.demo.constant.CookieConstant;
import com.myboot.demo.constant.RedisConstant;
import com.myboot.demo.exception.SellerAuthorizeException;
import com.myboot.demo.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 韦溜溜
 * @Description:
 * @Date: Create in 19:41 2019/1/12
 */
@Aspect
@Component
@Slf4j
public class SellerAuthorizeAspect {
//
//    @Autowired
//    private StringRedisTemplate template;
//
//    @Pointcut("execution(public * com.myboot.demo.controller.Seller*.*(..))"+
//            "&& !execution(public * com.myboot.demo.controller.SellerUserController.*(..))")
//    public void verify(){}
//
//    @Before("verify()")
//    public void doVerify(){
//        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        //查询cookie
//        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
//        if(cookie == null){
//            log.warn("【登陆验证】Cookie中查询不到token");
//            throw new SellerAuthorizeException();
//        }
//        //在redis中查询
//        String tokenValue = template.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
//        if(tokenValue == null){
//            log.warn("【登陆验证】Redis中查询不到token");
//            throw new SellerAuthorizeException();
//        }
//    }
}
